package WEB.Servlet.Actions;

import DB.dao.PersonDao;
import DB.dto.DoctorDto;
import DB.dto.NurseDto;
import DB.dto.PatientDto;
import DB.dto.PersonDto;
import DB.exceptions.PersonDaoException;
import DB.factory.PersonDaoFactory;
import personal.Doctor;
import personal.Nurse;
import personal.Patient;
import personal.Person;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: Vanil
 * Date: 06.12.2010
 * Time: 4:41:41
 */
public class ShowAction implements Action{
    public void writeToResponseStream(HttpServletResponse response, String output) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        if (id == null)
        {
            Person person = (Person)request.getSession().getAttribute("sessionUser");
            id = person.getIdPerson()+"";
        }
        PersonDao dao = PersonDaoFactory.create();
        PersonDto dto = null;
        try {
            dto = dao.findByPrimaryKey(new Integer(id));
            if(dto!=null)
            {
                Person contactUser=null;
                String url = "";
                if(dto instanceof DoctorDto)
                {
                    contactUser = new Doctor((DoctorDto)dto);
                    url = "/Doctor.jsp";
                }
                else
                    if(dto instanceof NurseDto)
                    {
                        contactUser = new Nurse((NurseDto)dto);
                        url = "/Nurse.jsp";
                    }
                    else
                        if(dto instanceof PatientDto)
                        {
                             contactUser = new Patient((PatientDto)dto);
                             url = "/Patient.jsp";
                        }
                request.setAttribute("displayedUser",contactUser);
                return url;
            }
            else
                request.getSession().setAttribute("errorMessage","Not correct user index");
        } catch (PersonDaoException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            request.getSession().setAttribute("errorMessage",e.getMessage());
        }
        return "/error.jsp";
        
        //String id = request.getParameter("id");

    }
}